<?php
/**
* MenuModel.php
* Contains CRUD settings for menu management.
*/

require_once("Menu.php");

class MenuModel
{
	private $databaseConnection;
	
	public function __construct( $databaseConnection )
	{
		$this->databaseConnection = $databaseConnection;
	}
	
	/**
	* SelectAllMenus
	* Fetching the menus from the database.
	*
	* @return Array $menuArray containing menu references 
	*
 	*/
	public function SelectAllMenus()
	{
		$menuArray = array();
		
		if ( $stmt = $this->databaseConnection->PrepareStatement( "SELECT * FROM menus" ) ) 
		{	
		    $stmt->execute();
		
		    $stmt->bind_result( $menuID, $name );
		 	
		 	while ( $stmt->fetch() ) 
		 	{
		        $menu = new Menu( $menuID, $name );
		        
		        $menuArray[] = $menu;
		    }
		    
		    $stmt->close();
		}
		
		return $menuArray;
	}
	
	/**
	* SaveMenu
	* Saves a menu.
	*
	* @param string $name The Name input witch the function saves to the database.
	*
	*/
	public function SaveMenu( $name )
	{
		if ( $stmt = $this->databaseConnection->PrepareStatement( "INSERT INTO menus (Name) VALUES(?)" ) ) 
		{
		    $stmt->bind_param( "s", $name );
		
		    $stmt->execute();
		
		    $stmt->close();
		}
		
	}
	
	/**
	* RemoveMenu
	* Remove a menu.
	*
	* @param int $menuID The ID witch the function need to know which menu to delete.
	*
	*/
	public function RemoveMenu( $menuID )
	{	
		if ( $stmt = $this->databaseConnection->PrepareStatement( "DELETE FROM menus WHERE MenuID = ?" ) ) 
		{	
			$stmt->bind_param( "i", $menuID );

		    $stmt->execute();

		    $stmt->close();
		}
	}
	
}